package liuyang.bigeventserver.modules.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import liuyang.bigeventserver.common.Id;
import liuyang.bigeventserver.common.R;
import liuyang.bigeventserver.common.pagination.ResultPage;
import liuyang.bigeventserver.modules.article.entity.Article;
import liuyang.bigeventserver.modules.article.mapper.ArticleMapper;
import liuyang.bigeventserver.modules.article.vo.QueryArticle;
import liuyang.bigeventserver.modules.article.vo.ResultArticle;
import liuyang.bigeventserver.modules.security.vo.SysUserDetails;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.security.Security;
import java.time.LocalDateTime;

/**
 * @author xconf
 * @since 2024/3/3
 */
@RestController
@RequestMapping("article")
@Slf4j
@AllArgsConstructor
public class ArticleController {
    private final ArticleMapper articleMapper;

    /**
     * 新增文章
     * https://www.bilibili.com/video/BV14z4y1N7pg/?p=32&vd_source=8bd7b24b38e3e12c558d839b352b32f4
     * {
     * title
     * content
     * coverImg     @URL
     * state        已发布|草稿
     * categoryId
     * }
     */
    @PostMapping("add")
    public R add(@RequestBody @Validated(Article.Add.class) Article article) {
        final SysUserDetails principal = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userid = principal.getUserid();
        article.setId(Id.nextId());// id
        article.setCreateUser(userid);// 新建文章的用户
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.insert(article);
        return R.ok("文章添加成功");
    }

    @GetMapping("delete")
    public R delete(Long id) {
        // TODO 可以考虑使用eq的condition
        final LambdaQueryWrapper<Article> articleLambdaQueryWrapper = Wrappers.<Article>lambdaQuery();
        articleLambdaQueryWrapper.eq(Article::getId, id);
        final int deleteRow = articleMapper.delete(articleLambdaQueryWrapper);
        if (0 == deleteRow) {
            return R.error("文章不存在");
        }

        return R.ok("文章删除成功");
    }

    // update
    // TODO

    // detail
    // TODO

    // list
    // https://www.bilibili.com/video/BV14z4y1N7pg/?p=34&spm_id_from=pageDriver&vd_source=8bd7b24b38e3e12c558d839b352b32f4
    /**
     * 没有采用视频中的queryString方案。
     * {
     *     pageNum      当前页码(必须)
     *     pageSize     每页条数(必须)
     *     categoryId   文章分类ID
     *     state        发布状态        已发布 | 草稿
     * }
     *
     * 提要：
     * 如果使用了queryString方案,在非必须字段上可以增加@RequestParam(required=false)进行修饰。
     */
    @PostMapping("list")
    public R list(@RequestBody @Validated QueryArticle queryArticle) {
        // userid
        final SysUserDetails principal = (SysUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 查询条件
        final LambdaQueryWrapper<Article> articleLambdaQueryWrapper = Wrappers.<Article>lambdaQuery();
        articleLambdaQueryWrapper.eq(queryArticle.getCategoryId() != null, Article::getCategoryId, queryArticle.getCategoryId());
        articleLambdaQueryWrapper.eq(StringUtils.isNotEmpty(queryArticle.getState()), Article::getState, queryArticle.getState());
        articleLambdaQueryWrapper.eq(Article::getCreateUser, principal.getUserid());
        // 分页条件
        Page<Article> articlePage = new Page<>(queryArticle.getPageNum(), queryArticle.getPageSize());
        // 执行查询
        final Page<Article> articlePageResult = articleMapper.selectPage(articlePage, articleLambdaQueryWrapper);
        // 拼接结果
        ResultPage<Article> result = new ResultPage<>();
        result.setTotal(articlePageResult.getTotal());
        result.setItems(articlePageResult.getRecords());
        return R.ok().put("data", result);
    }

}